<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Kundsidor extends BIT_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function __construct()
	{
		parent::__construct();

		$this->load->library('tank_auth');

		if(! $this->tank_auth->is_logged_in()){
			redirect('/kundsidor');
		}

		$this->header_views = array('common/header');
		$this->footer_views = array('common/footer');

		$this->load_standards();
	}

	public function index()
	{
		$this->load->model('kunder_order');
		$this->view_data['orders'] = $this->kunder_order->getOrdersByCustomer( $this->tank_auth->get_email() );
		$this->v('kundsidor/home');
	}

	public function settings()
	{
		$this->load->library('tank_auth');

		if($this->input->post('updateprofile')){
			$toupdate = array('fnamn','enamn','telefon','gadress','postnr','postort');
			$data = new stdClass();
			foreach($_POST as $k=>$v){
				if(in_array($k, $toupdate))
					$data->{$k} = $v;
			}
			$updated = $this->users->update_profile($this->tank_auth->get_user_id(), $data);
		}

		$this->view_data['profile'] = $this->users->get_profile( $this->tank_auth->get_user_id() );
		$this->v('kundsidor/settings');
	}

	private function load_standards(){

		$sliderdata = array('products' => $this->produkter->getSliderProducts());	
		$this->view_data['slider_html'] = $this->load->view('pages/slider', $sliderdata, TRUE);
		$this->view_data['widgets'] = array();

		$this->load->library("cart_handler");
		$cart_data = array(
			'summary'	=> $this->cart_handler->get_count(),
			'price'		=> $this->cart_handler->get_total(),
			'display_css' => $this->cart_handler->get_count() > 0 ? "show" : "hide"
		);
		$this->view_data['cart_html'] = $this->load->view('widgets/cart', $cart_data, TRUE);

		$this->load->helper('url');

		$mnu_data['mnu_order_css']		= $this->uri->segment(2) === "orderhistorik" ? " active " : "";
		$mnu_data['mnu_settings_css']	= $this->uri->segment(2) === "installningar" ? " active " : "";

		$this->view_data['widgets'] = array(
			$this->load->view('widgets/customer_menu', $mnu_data, TRUE)
		);
	}

}